﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SQLTouch.Service.DataContracts;

namespace SQLTouch.Client.Metro.ViewModel.Explore
{
    public class AddQueryVM : VMBase
    {
        public class TableVM : VMBase
        {
            private readonly TableData data;

            public TableVM(TableData data)
            {
                this.data = data;
            }

            public TableData Data
            {
                get { return data; }
            }

            public string DisplayString
            {
                get { return data.Name.ToString(); }
            }
        }

        private readonly IFacade facade;
        private readonly IPopupService popup;
        private readonly DatabaseData database;
        private readonly List<TableVM> tables;
        private TableVM selectedTable;
        private readonly RelayCommand add;
        private readonly RelayCommand cancel;

        public AddQueryVM(IFacade facade, IPopupService popup, DatabaseData database)
        {
            this.facade = facade;
            this.popup = popup;
            this.database = database;
            this.tables = facade.GetSchema(database.ConnectionString).Tables.Select(p => new TableVM(p)).ToList();
            this.selectedTable = tables.FirstOrDefault();
            this.add = new RelayCommand(DoAdd, CanAdd);
            this.cancel = new RelayCommand(DoCancel);
        }

        public IEnumerable<TableVM> Tables
        {
            get { return tables; }
        } 

        public TableVM SelectedTable
        {
            get { return selectedTable; }
            set
            {
                if (selectedTable != value)
                {
                    selectedTable = value;
                    RaisePropertyChanged("SelectedTable");
                    add.RaiseCanExecuteChanged();
                }
            }
        }

        public RelayCommand Add
        {
            get { return add; }
        } 

        public RelayCommand Cancel
        {
            get { return cancel; }
        }

        private void DoCancel()
        {
            popup.ClosePopup();
        }

        private void DoAdd()
        {
            QueryData query = new QueryData(Guid.NewGuid(), database.Id, selectedTable.Data.Name, selectedTable.Data.Columns.Select(p => new SelectData(p.Name, AggregateTypeData.None)));
            try
            {
                facade.AddQuery(query);
            }
            catch (Exception ex)
            {
                facade.ShowUnexpectedError("Unable to add the query.", ex);
                return;
            }
            popup.ClosePopup();
        }

        private bool CanAdd()
        {
            return selectedTable != null;
        }
    }
}
